import { queryInstOrderInfo } from '../../services/inspect-project'
import { updateInstInfo } from '../../services/inspect'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderProjectList:[],
    orderId:"",
    orderType:"",
    showDialogConfirm:false,
    dialogMsg:"",
    instId:"",
    url:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(!options){
      return
    }

    let orderId = options.orderId
    let orderType = options.type
    this.getOrderInstList(orderId)
    this.setData({
      orderId,
      orderType
    })

  },

  onShow(){
    let orderId = this.data.orderId || ""
    if(orderId){
      this.getOrderInstList(orderId)
    }
  },

  async getOrderInstList(orderId){
    let res = await queryInstOrderInfo({ orderId })
    if(res.code === 200){
      let orderProjectList = res.rows
      this.setData({
        orderProjectList
      })
    }
  },

  navToReport(event){
    console.log("event>>>", event)
    let id = event.currentTarget.dataset.instid
    let name = event.currentTarget.dataset.name
    let orderId = event.currentTarget.dataset.orderid
    let type = event.currentTarget.dataset.type
    let jsonUrl = event.currentTarget.dataset.url
    let tag = event.currentTarget.dataset.tag
    let status = event.currentTarget.dataset.status
    let item = event.currentTarget.dataset.item
    let url = ""
    let isOk = false

    if(status != 3){
      return
    }

    if(type == 1){
      url = `/pages/report-appearance/report-appearance?instId=${id}&orderId=${orderId}`
      if(item.itemJson){
        isOk = true
      }
    } else if(type == 2){
      url = `/pages/report-insulation/report-insulation?instId=${id}&orderId=${orderId}`
      if(item.itemJson){
        isOk = true
      }
    } else if(type == 3){
      // if(this.data.orderType){
      //   if(jsonUrl){
      //     url = `/pages_report/pages/report-self-check/report-self-check?jsonUrl=${jsonUrl}`
      //   }
      // } else {
      //   url = `/pages/report-charging/report-charging?instId=${id}&orderId=${orderId}`
      // }
      url = `/pages_report/pages/report-charging/report-charging?instId=${id}&orderId=${orderId}&jsonUrl=${jsonUrl}`
      if(jsonUrl){
        isOk = true
      }
    } else if(type == 4){
      url = `/pages_report/pages/report-discharge-all/report-discharge-all?instId=${id}&orderId=${orderId}&jsonUrl=${jsonUrl}`
      if(jsonUrl){
        isOk = true
      }
    }
    console.log("isOk>>>", isOk)
    if(isOk){
      if(!tag){
        this.setData({
          showDialogConfirm: true,
          dialogMsg: "是否确认支付?",
          url,
          instId:id
        })
      } else {
        wx.navigateTo({
          url,
        })
      }
    }
  
    // 外观检测
    // if(type == 1){
    //   wx.navigateTo({
    //     url: `/pages/report-appearance/report-appearance?instId=${id}&orderId=${orderId}`,
    //   })
    // } else if(type == 2){
    //   wx.navigateTo({
    //     url: `/pages/report-insulation/report-insulation?instId=${id}&orderId=${orderId}`,
    //   })
    // } else if(type == 3){
    //   if(this.data.orderType){
    //     if(jsonUrl){
    //       wx.navigateTo({
    //         url: `/pages_report/pages/report-self-check/report-self-check?jsonUrl=${jsonUrl}`,
    //       })
    //     }
    //   } else {
    //     wx.navigateTo({
    //       url: `/pages/report-charging/report-charging?instId=${id}&orderId=${orderId}`,
    //     })
    //   }
    // } else if(type == 4){
    //   wx.navigateTo({
    //     url: `/pages/report-discharge/report-discharge?instId=${id}&orderId=${orderId}`,
    //   })
    // }
  },

  async handlePay(){
    let id = this.data.instId
    let res = await updateInstInfo({
      id,
      tag: '1'
    })
    let url = this.data.url
    console.log("res>>", res)
    wx.navigateTo({
      url,
    })
  },

  handleViewReport(event){
    console.log("查看整车检测综合报告~")
    console.log("orderProjectList>>>", this.data.orderProjectList)
    let orderId = this.data.orderId
    let url = `/pages_report/pages/report-general/report-general?orderId=${orderId}`
    wx.navigateTo({
      url,
    })
  }
})